﻿using System;
using System.Web.Mvc;
using mBlog.Extensions;
using mBlog.Services;
using mBlog.ViewModels;
using mBlog.Models;
using mBlog.Helpers;
using MvcMiniProfiler;

namespace mBlog.Controllers
{
    public class PostController : BaseController
    {

        private IPostService postService;

        public PostController(IPostService postService)
        {
            this.postService = postService;
        }

        public new ActionResult View(string urlName)
        {
            var profiler = MiniProfiler.Current; // it's ok if this is null

            using (profiler.Step("Set page title"))
            {
                ViewBag.Title = "Home Page";
            }

            Post post = null;

            using (profiler.Step("Step A"))
            {
                post = postService.Single(urlName);
            }
            using (profiler.Step("Build ViewModel"))
            {
                if (post == null)
                {
                    return HttpNotFound();
                }

                PostView pv = new PostView()
                {
                    Post = post.MapTo<PostDetail>(),
                    Categories = post.Categories.MapTo<CategoryDetail>(),
                    Tags = post.Tags.MapTo<TagDetail>(),
                    AddComment = new CommentInput
                    {
                        PostId = post.ID,
                    }
                };
                return View(pv);
            }



        }

        public ActionResult AddComment(CommentInput commentInput)
        {
            Post post = postService.Single(commentInput.PostId);
            if (post == null)
                return HttpNotFound();

            ValidateCaptcha();

            if (!ModelState.IsValid)
                return CommentFailed(post, commentInput);

            var comment = commentInput.MapTo<Comment>();
            comment.CommentTime = DateTime.Now;

            postService.AddComment(commentInput.PostId, comment);

            return CommentSucceeded(post);
        }

        private ActionResult CommentFailed(Post post, CommentInput input)
        {
            if (Request.IsAjaxRequest())
                return Json(new { Success = false, message = ModelState.GetFirstErrorMessage() });

            var result = View(post.UrlName);
            var model = result as ViewResult;
            if (model != null)
            {
                var viewModel = model.Model as PostView;
                if (viewModel != null)
                    viewModel.AddComment = input;
            }

            return View("View", model.Model);
        }

        private ActionResult CommentSucceeded(Post post)
        {
            const string successMessage = "Your comment will be posted soon. Thanks!";

            if (Request.IsAjaxRequest())
                return Json(new { Success = true, message = successMessage });

            return RedirectToAction("View", new { urlName = post.UrlName });
        }

        private void ValidateCaptcha()
        {

            if (RecaptchaValidatorWrapper.Validate(ControllerContext.HttpContext))
                return;

            ModelState.AddModelError("CaptchaNotValid",
                                     "You did not type the verification word correctly. Please try again.");
        }
    }
}
